package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.LitemallUserAccountMapper;
import org.linlinjava.litemall.db.dao.LitemallUserMapper;
import org.linlinjava.litemall.db.dao.ex.UserMapperEx;
import org.linlinjava.litemall.db.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class LitemallUserService {
    @Resource
    private LitemallUserMapper userMapper;

    @Resource
    private UserMapperEx userMapperEx;

    @Resource
    private LitemallUserAccountMapper userAccountMapper;

    public LitemallUser findById(Integer userId) {
        return userMapper.selectByPrimaryKey(userId);
    }

    public UserVo findUserVoById(Integer userId) {
        LitemallUser user = findById(userId);
        UserVo userVo = new UserVo();
        userVo.setNickname(user.getNickname());
        userVo.setAvatar(user.getAvatar());
        return userVo;
    }

    public LitemallUser queryByOid(String openId) {
        LitemallUserExample example = new LitemallUserExample();
        example.or().andWeixinOpenidEqualTo(openId).andDeletedEqualTo(false);
        return userMapper.selectOneByExample(example);
    }

    public void add(LitemallUser user) {
        user.setAddTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        userMapper.insertSelective(user);
    }

    public int updateById(LitemallUser user) {
        user.setUpdateTime(LocalDateTime.now());
        return userMapper.updateByPrimaryKeySelective(user);
    }

    public List<LitemallUser> querySelective(String username, String mobile, Integer brokerage, Integer page, Integer size, String sort, String order) {
        LitemallUserExample example = new LitemallUserExample();
        LitemallUserExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        if (!StringUtils.isEmpty(mobile)) {
            criteria.andMobileEqualTo(mobile);
        }
        if (Objects.nonNull(brokerage)) {
            criteria.andBrokerageEqualTo(brokerage);
        }
        criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, size);
        return userMapper.selectByExample(example);
    }

    public List<LitemallUserVo> queryUserPage(String username, String mobile, Integer brokerage, Integer page, Integer size, String sort, String order) {
        PageHelper.startPage(page, size);
        return userMapperEx.queryUserPage(username, mobile, brokerage, sort, order);
    }

    public int count() {
        LitemallUserExample example = new LitemallUserExample();
        example.or().andDeletedEqualTo(false);

        return (int) userMapper.countByExample(example);
    }

    public List<LitemallUser> queryByUsername(String username) {
        LitemallUserExample example = new LitemallUserExample();
        example.or().andUsernameEqualTo(username).andDeletedEqualTo(false);
        return userMapper.selectByExample(example);
    }

    public boolean checkByUsername(String username) {
        LitemallUserExample example = new LitemallUserExample();
        example.or().andUsernameEqualTo(username).andDeletedEqualTo(false);
        return userMapper.countByExample(example) != 0;
    }

    public List<LitemallUser> queryByMobile(String mobile) {
        LitemallUserExample example = new LitemallUserExample();
        example.or().andMobileEqualTo(mobile).andDeletedEqualTo(false);
        return userMapper.selectByExample(example);
    }

    public List<LitemallUser> queryByOpenid(String openid) {
        LitemallUserExample example = new LitemallUserExample();
        example.or().andWeixinOpenidEqualTo(openid).andDeletedEqualTo(false);
        return userMapper.selectByExample(example);
    }

    public void deleteById(Integer id) {
        userMapper.logicalDeleteByPrimaryKey(id);
    }


    /**
     * 审批代理申请
     *
     * @param userAccount
     */
    public void approveAgency(Integer userId, Integer settlementRate, String shareUrl, Integer brokerage) {
        //获取账户数据
        LitemallUserAccountExample example = new LitemallUserAccountExample();
        example.or().andUserIdEqualTo(userId);

        LitemallUserAccount dbAccount = userAccountMapper.selectOneByExample(example);
        if (dbAccount == null) {
            throw new RuntimeException("申请账户不存在");
        }
        dbAccount.setShareUrl(shareUrl);
        if (!StringUtils.isEmpty(settlementRate)) {
            dbAccount.setSettlementRate(settlementRate);
        }
        dbAccount.setModifyTime(LocalDateTime.now());
        userAccountMapper.updateByPrimaryKey(dbAccount);

        //更新会员状态和类型
        LitemallUser user = findById(userId);
        user.setBrokerage(brokerage);//区域代理用户
        user.setStatus((byte) 0);//正常状态
        updateById(user);
    }

    public LitemallUserAccount detailApproveByUserId(Integer userId) {
        // 获取账户数据
        LitemallUserAccountExample example = new LitemallUserAccountExample();
        example.or().andUserIdEqualTo(userId);

        LitemallUserAccount dbAccount = userAccountMapper.selectOneByExample(example);
        return dbAccount;
    }


    /**
     * 取消代理
     *
     * @param userId
     * @param brokerage
     */
    public void cancelAgency(Integer userId, Integer brokerage) {
        //更新会员状态和类型
        LitemallUser user = findById(userId);
        user.setBrokerage(brokerage);//区域代理用户
        user.setStatus((byte) 0);//正常状态
        updateById(user);
    }

}
